import Vue from 'vue'
import VueRouter from 'vue-router'
import Main from '../views/Main.vue'
import Login from '../views/Login.vue'
import CategoryEdit from '../views/CategoryEdit.vue'
import CategoryList from '../views/CategoryList.vue'

import ItemEdit from '../views/ItemEdit.vue'
import ItemList from '../views/ItemList.vue'

import HeroEdit from '../views/HeroEdit.vue'
import HeroList from '../views/HeroList.vue'

import ArticleEdit from '../views/ArticleEdit.vue'
import ArticleList from '../views/ArticleList.vue'

import AdsEdit from '../views/AdsEdit.vue'
import AdsList from '../views/AdsList.vue'

import AdminUserEdit from '../views/AdminUserEdit.vue'
import AdminUserList from '../views/AdminUserList.vue'

Vue.use(VueRouter)

const routes = [
  {
    path:'/login',
    name: 'login',
    component: Login,
    meta: { notRequireAuth: true }  // 不需要验证路由权限
  },
  {
    path: '/',
    name: 'Main',
    component: Main,
    redirect: '/category/list',
    children: [
      { name: 'category-create', path: '/category/create', component: CategoryEdit },
      { name: 'category-edit', path: '/category/edit/:id', component: CategoryEdit, props: true },
      { name: 'category-list', path: '/category/list', component: CategoryList },

      { name: 'item-create', path: '/item/create', component: ItemEdit },
      { name: 'item-edit', path: '/item/edit/:id', component: ItemEdit, props: true },
      { name: 'item-list', path: '/item/list', component: ItemList },

      { name: 'hero-create', path: '/hero/create', component: HeroEdit },
      { name: 'hero-edit', path: '/hero/edit/:id', component: HeroEdit, props: true },
      { name: 'hero-list', path: '/hero/list', component: HeroList },

      { name: 'article-create', path: '/article/create', component: ArticleEdit },
      { name: 'article-edit', path: '/article/edit/:id', component: ArticleEdit, props: true },
      { name: 'article-list', path: '/article/list', component: ArticleList },

      { name: 'ads-create', path: '/ads/create', component: AdsEdit },
      { name: 'ads-edit', path: '/ads/edit/:id', component: AdsEdit, props: true },
      { name: 'ads-list', path: '/ads/list', component: AdsList },

      { name: 'adminuser-create', path: '/adminuser/create', component: AdminUserEdit },
      { name: 'adminuser-edit', path: '/adminuser/edit/:id', component: AdminUserEdit, props: true },
      { name: 'adminuser-list', path: '/adminuser/list', component: AdminUserList },
    ]
  }
]

const router = new VueRouter({
  routes
})


// 路由守卫
router.beforeEach((to, from, next) => {
  if(!to.meta.notRequireAuth && !localStorage.getItem('token')) {
    return next('/login')
  }
  next()
})

export default router
